home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Applications / ARTAbrot 1.0 / ComUtil_ARTAbrot.c < prev    next >
Text File  |  1993-08-23  |  16KB  |  405 lines

  1. /*   ComUtil_ARTAbrot                            Common */
  2.  
  3. /* Name:  ComUtil_ARTAbrot.c */
  4. /* History: 8/18/93 Original by George Warner */
  5.  
  6.  
  7. #include "ComUtil_ARTAbrot.h"    /* Common */
  8. #include "SysEqu.h"    /* System equates */
  9. /* ======================================================= */
  10.  
  11. Boolean    Doing_MovableModal;                        /* For Movable Modal dialogs */
  12. UserEventHRec    UserEventList;                    /* User Event record list start */
  13. EventRecord    myEvent;                            /* Event record for all events */
  14. Boolean    WNE;                                    /* WaitNextEvent trap is available */
  15. short    SleepValue;                                /* Sleep value for Wait on events */
  16. Boolean    doneFlag;                                /* Exit program flag */
  17. TEHandle    theInput;                                /* Used in text edit selections */
  18. Rect    tempRect;                                    /* Temporary rect, not for long term use */
  19. Str255    sTemp;                                    /* Temporary string, not for long term use */
  20. Boolean        HasColorQD;                            /* Flag for Color QuickDraw being available */
  21. Boolean        HasFPU;                                /* Flag for Floating Point Math Chip being available */
  22. Boolean        HoldOffUserEvents;                    /* Flag for holding off UserEvents */
  23. Boolean        InTheForeground;                    /* Flag for running in MultiFinder foreground */
  24. short        tempChar;                                /* Temporary use character */
  25. long        LTemp;                                    /* Temporary use long variable */
  26. RGBColor        Black_ForeColor,White_BackColor;    /* Standard colors */
  27.  
  28. SFTypeList    typeList;                            /* For use in opening files */
  29. OSErr        ErrorCode;                            /* For use in reading and writing files */
  30. SFReply    Reply;                                    /* For use in getting file names */
  31. StandardFileReply    theStandardFileReply;            /* For use in getting file names */
  32. short    inputRefNum;                                /* For the input file */
  33. short    outputRefNum;                                /* For the output file */
  34. Str255 inputFileName;                                /* For the input file */
  35. Str255 outputFileName;                                /* For the output file */
  36.  
  37. RgnHandle    cursorRgn;                            /* Cursor region for WaitNextEvent */
  38. Boolean        HasAppleEvents;                        /* Whether AppleEvents are available */
  39. Boolean        HasAliasMgr;                        /* Whether AliasMgr is available */
  40. Boolean        HasEditionMgr;                        /* Whether EditionMgr is available */
  41. short        ReplyMode;                            /* Reply mode for AppleEvents */
  42. Boolean        HasNewStdFile;                        /* Whether HasNewStdFile is available */
  43. Boolean        HasPPCToolbox;                        /* Whether PPCToolbox is available */
  44. Boolean        Has32BitQuickDraw;                    /* Whether 32Bit QuickDraw is available */
  45. Boolean        HasGestalt;                            /* Whether Gestalt is available */
  46.  
  47. THPrint    hPrint;                                    /* Printer record */
  48. Boolean    PrinterIsOpen;                            /* Whether printer is open or not */
  49. short    PageCount;                                /* For adding in page number */
  50. short    LineCount;                                /* For printing */
  51. short    PrinterVRes,PrinterHRes;                    /* Printer resolution */
  52. short    MaxPrintCharacters;                        /* Max characters on one line */
  53. short    MaxLines;                                    /* Maximum lines in one page */
  54. TPPrPort        pPrPort;                            /* Printer port */
  55. TPrStatus        prStatus;                            /* Printing status */
  56. short    CopyCount;                                /* Number of copies to do */
  57.  
  58. MenuHandle    Menu_Apple;                            /* Menu handle */
  59. MenuHandle    Menu_File;                            /* Menu handle */
  60. MenuHandle    Menu_Commands;                        /* Menu handle */
  61.  
  62. /* Window variables for the window titled  "ARTAbrot" */
  63. WindowPtr    WPtr_ARTAbrot;                        /* Window pointer */
  64.  
  65. /* Variables for the modeless dialog titled  "About ARTAbrot" */
  66. WindowPtr    WPtr_About_ARTAbrot;                    /* Modeless Dialog window pointer */
  67.  
  68. /* Variables for the alert titled  "Alert" */
  69.  
  70. /* Variables for the modeless dialog titled  "Enter Coordinates" */
  71. WindowPtr    WPtr_Enter_Coordinat;                    /* Modeless Dialog window pointer */
  72. Rect    DTE_Rect_Edit_Text4;                        /* ...Edit text rect */
  73. Rect    DTE_Rect_Edit_Text3;                        /* ...Edit text rect */
  74. Rect    DTE_Rect_Edit_Text2;                        /* ...Edit text rect */
  75. Rect    DTE_Rect_Edit_Text;                        /* ...Edit text rect */
  76.  
  77.  
  78. /* Mandelbrot coordinates. */
  79. float    fxcenter = 0.0;    /* X center of fractal. */
  80. float    fycenter = 0.0;    /* Y center of fractal. */
  81. float    fwidth = 4.0;        /* Width of fractal. */
  82. int        fiters = 128;    /* Maximum number of iterations. */
  83. int        new_coordinates = TRUE;
  84.  
  85.  
  86. /* Prototypes */
  87.  
  88. /* Handle an activate of the window */
  89. void DoActivate(void);
  90.  
  91. /* See if WaitNextEvent is available */
  92. Boolean IsWNEIsImplemented(void);
  93.  
  94. /* Check for user events */
  95. void Handle_User_Event(void);
  96.  
  97. /* Handle a hit in the window */
  98. void DoInContent(WindowPtr whichWindow,EventRecord *myEvent);
  99.  
  100. #define    fnfErr        -43
  101.  
  102. /* ======================================================= */
  103.  
  104.  
  105. /* Routine: PStrCopy */
  106. /* Purpose: Copy Pascal strings */
  107.  
  108. void PStrCopy(Str255 *SourceString, Str255 *DestString)/* Copy Pascal strings */
  109. {
  110.  
  111. *DestString[0] = *SourceString[0];                    /* Get the string size */
  112. if (*DestString[0] != 0)                            /* Do if there is anything in the string */
  113.     BlockMove(SourceString,DestString,*DestString[0]+1);
  114. }
  115.  
  116. /* ======================================================= */
  117.  
  118. /* Routine: PStrCat */
  119. /* Purpose: Concat Pascal strings */
  120.  
  121. void PStrCat(Str255 *SourceString, Str255 *DestString)/* Concat Pascal strings */
  122. {
  123. short    OldDestLength,NewDestLength,SizeFromSource;
  124.  
  125. OldDestLength = *DestString[0];                    /* Get the string old length */
  126. SizeFromSource = *SourceString[0];                    /* Get the source length */
  127. NewDestLength = OldDestLength + SizeFromSource;    /* Get the string new length */
  128. if (NewDestLength > 255)                            /* Compare to length of the final string */
  129.     {
  130.     NewDestLength = 255;                            /* The string final length */
  131.     SizeFromSource = 255 - OldDestLength;            /* The string final length */
  132.     }                                            /* End of IF */
  133. *DestString[0] = NewDestLength;                    /* Set the string length */
  134. if (SizeFromSource > 0)                            /* Do if there is anything in the string */
  135.     BlockMove((Ptr)((long)SourceString + 1L),(Ptr)((long)DestString+OldDestLength+1L),SizeFromSource);
  136. }
  137.  
  138. /* ======================================================= */
  139.  
  140. /* Routine: PStrCmp */
  141. /* Purpose: Compare Pascal strings for exact match, case sensitive */
  142.  
  143. Boolean PStrCmp(StringPtr Source1String,StringPtr Source2String)/* Compare Pascal strings */
  144. {
  145. short        theLength,Index;
  146. Boolean        theSame;
  147.  
  148.     theSame = false;                                    /* Init to different */
  149.     theLength = *Source1String++;                        /* Get length of one string */
  150.     if (theLength == *Source2String++)                    /* Compare to length of other string, easy and quick check */
  151.     {
  152.         theSame = true;                                /* Init to same, now we will check each character */
  153.         for (Index=1; Index <= theLength; Index++)        /* Compare to length of other string, easy and quick check */
  154.         {
  155.             if (*Source1String++ != *Source2String++)    /* See if this character is the same */
  156.             {
  157.                 theSame = false;                        /* Different */
  158.             }                                    /* End of IF */
  159.         }                                        /* End of IF */
  160.     }                                            /* End of IF */
  161.     return(theSame);
  162. }
  163.  
  164. /* ======================================================= */
  165.  
  166. /* Routine: CheckTrapAvailable */
  167. /* Purpose: See if trap is available, non-available traps all have a unique address */
  168.  
  169. Boolean CheckTrapAvailable (short trapNumber,short tType)/* See if a trap is available */
  170. {
  171. #define UnimplementedTrapNumber     0xA89F            /* Unimplemented trap number */
  172. Boolean    theResult;
  173.  
  174. theResult = (NGetTrapAddress(trapNumber, tType) != GetTrapAddress(UnimplementedTrapNumber));/* Check the two traps */
  175. return(theResult);
  176. }
  177.  
  178. /* ======================================================= */
  179.  
  180. /* Routine: GetUserEvent */
  181. /* Purpose: See if any user events are available */
  182.  
  183. void GetUserEvent(UserEventPRec TheUserEvent)
  184. UserEventHRec    NextUserEvent;                    /* The next user event */
  185.  
  186. TheUserEvent->ID = UserEvent_None;                    /* Set ID to no events are available */
  187. if (UserEventList != NIL)                            /* Get first entry in the list */
  188.     {
  189.     HLock((Handle)UserEventList);                    /* Lock for safety */
  190.     TheUserEvent->ID = (*UserEventList)->ID;        /* The event ID */
  191.     TheUserEvent->ID2 = (*UserEventList)->ID2;        /* The optional ID */
  192.     TheUserEvent->Data1 = (*UserEventList)->Data1;/* The optional data */
  193.     TheUserEvent->Data2 = (*UserEventList)->Data2;/* The optional data */
  194.     TheUserEvent->theHandle = (*UserEventList)->theHandle;/* The optional handle */
  195.     NextUserEvent = (*UserEventList)->Next;        /* The next list */
  196.  
  197.     DisposHandle((Handle)UserEventList);            /* Remove this list item */
  198.     UserEventList = NextUserEvent;                    /* Make the next item the new first item */
  199.     }                                            /* End of IF */
  200. }
  201.  
  202. /* ======================================================= */
  203.  
  204. /* Routine: Add_UserEvent */
  205. /* Purpose: Add a user event */
  206.  
  207. void Add_UserEvent(short ID1,short  ID2,long Data1,long Data2,Handle  theHandle)
  208. UserEventHRec    NewUserEvent;                        /* The new user event */
  209. UserEventHRec    theUserEvent;                        /* The user event */
  210.  
  211. NewUserEvent = (UserEventHRec)NewHandle(sizeof(UserEventRec));/* Allocate a record */
  212. if (NewUserEvent != NIL)                            /* Only do if we got the new record */
  213.     {
  214.     HLock((Handle)NewUserEvent);                    /* Lock for safety */
  215.     (*NewUserEvent)->ID = ID1;                        /* The event ID */
  216.     (*NewUserEvent)->ID2 = ID2;                    /* The optional ID */
  217.     (*NewUserEvent)->Data1 = Data1;                /* The optional data */
  218.     (*NewUserEvent)->Data2 = Data2;                /* The optional data */
  219.     (*NewUserEvent)->theHandle = theHandle;        /* The optional handle */
  220.     (*NewUserEvent)->Next = NIL;                    /* No next item after this one */
  221.  
  222.     if (UserEventList == NIL)                        /* See if anyone is in the list yet */
  223.         {
  224.         UserEventList = NewUserEvent;                /* Make this one the first in the list */
  225.         }
  226.     else
  227.         {
  228.         theUserEvent = UserEventList;                /* Get the first one */
  229.         while ((*theUserEvent)->Next != NIL)        /* Get the next one */
  230.             {
  231.             theUserEvent = (*theUserEvent)->Next;
  232.             }
  233.         (*theUserEvent)->Next = NewUserEvent;        /* Tack on to the end */
  234.         }                                        /* End of IF */
  235.     }                                            /* End of IF */
  236. }
  237.  
  238. /* ======================================================= */
  239.  
  240. /* Routine: Play_The_Sound */
  241. /* Purpose: Play my sound */
  242.  
  243. void Play_The_Sound(short ResID)
  244. Handle    ResHandle;                                /* The sound resource handle */
  245. short    SndResourceID;                            /* Sound resource ID */
  246. Str255    SndName;                                    /* Sound name*/
  247. ResType    theType;                                /* Resource type */
  248. OSErr    MyErr;                                    /* Error flag */
  249.  
  250. ResHandle = Get1Resource('snd ', ResID);            /* See if the resource is out there */
  251. if (ResHandle != NIL)                                /* Only do if we got the sound */
  252.     {
  253.     GetResInfo(ResHandle, &SndResourceID, &theType, &SndName);
  254.     MyErr = SndPlay(NIL, ResHandle, FALSE);
  255.     ReleaseResource(ResHandle);
  256.     }                                            /* End of IF */
  257. }
  258.  
  259. /* ======================================================= */
  260.  
  261. /* Routine: Get_TE_String */
  262. /* Purpose: This is a routine used to get a string from a TE area, limited to 250 characters */
  263.  
  264. void Get_TE_String(TEHandle theTEArea,Str255 *theString)
  265. short Index;                                        /* Use to loop thru the characters */
  266. short TitleLength;                                    /* Number of characters to do */
  267. CharsHandle theCharsHandle;                        /* Used to get global edit text */
  268. Ptr theStringPtr;                                    /* Pointer to the string, byte level */
  269. Ptr theTECharPtr;                                    /* Pointer to the string, byte level */
  270.  
  271. theCharsHandle = TEGetText(theTEArea);                /* Get the character handle */
  272. HLock ( ( Handle ) theCharsHandle );                /* Lock it for safety */
  273. theTECharPtr = (Ptr)*theCharsHandle;                /* Get string Ptr */
  274. TitleLength = (*theTEArea)->teLength;                /* Get the number of characters */
  275. *theString[0] = 0;                                    /* Start with an empty string */
  276. if (TitleLength > 0) 
  277.     {
  278.     theStringPtr = (Ptr)((long)theString + (long)1);/* Start of the string data */
  279.     if (TitleLength > 250) 
  280.         TitleLength = 250;
  281.     for (Index = 0; Index < TitleLength; Index++)
  282.         *theStringPtr++ = (char)(*theTECharPtr++);
  283.     *theString[0] = TitleLength;
  284.     }                                            /* End of IF */
  285. }
  286.  
  287. /* ======================================================= */
  288.  
  289. /* Routine: Make_TE_Area */
  290. /* Purpose: This is a routine used to create a TE area */
  291.  
  292. void Make_TE_Area(TEHandle *theTEArea,Rect *Position,short theFontSize,short theFont,short DefaultStringID)
  293. {
  294. FontInfo    ThisFontInfo;                            /* Use to get the font data */
  295.  
  296. TextSize(theFontSize);                                /* Set the size */
  297. TextFont(theFont);                                    /* Set the font */
  298. GetFontInfo(&ThisFontInfo);                        /* Get Ascent height for positioning */
  299. TextSize(12);                                        /* Restore the size */
  300. TextFont(applFont);                                /* Restore the font */
  301.  
  302. tempRect = *Position;                                /* Get the rect */
  303. FrameRect(&tempRect);                                /* Frame this TE area */
  304. InsetRect(&tempRect, 3, 3);                        /* Indent for TE inside of box */
  305. *theTEArea = TENew(&tempRect, &tempRect);            /* Create the TE area */
  306. if (theInput != NIL)                                /* See if there is already a TE area */
  307.     TEDeactivate(theInput);                    /* Yes, so turn it off */
  308.  
  309. theInput = *theTEArea;                                /* Activate the TE area */
  310. HLock((Handle)*theTEArea);                            /* Lock the handle before using it */
  311. (**theTEArea)->txFont = theFont;                    /* Font to use for the TE area */
  312. (**theTEArea)->fontAscent = ThisFontInfo.ascent;    /* Font ascent */
  313. (**theTEArea)->lineHeight = ThisFontInfo.ascent + ThisFontInfo.descent + ThisFontInfo.leading;/* Font ascent + descent + leading */
  314. HUnlock((Handle)*theTEArea);                        /* UnLock the handle when done */
  315. GetIndString(sTemp, DefaultStringID, 1);            /* Get the default string */
  316. TESetText(&sTemp[1], sTemp[0], theInput);            /* Place default text in the TE area */
  317. TEActivate(theInput);                                /* Make the TE area active */
  318. }
  319.  
  320. /* ======================================================= */
  321.  
  322. /* Routine: SetupTheItem */
  323. /* Purpose: Setup a dialog or alert item */
  324.  
  325. void SetupTheItem(DialogPtr theDialog,short ItemID,Boolean SizeIt,Boolean ShowIt,
  326. Boolean EnableIt,Boolean SetTheMax,Rect *thePosition,long ExtraData,short StringID)
  327. {
  328. Rect        tempRect;                            /* Temporary rectangle */
  329. short    DType;                                    /* Type of dialog item */
  330. Handle    DItem;                                    /* Handle to the dialog item */
  331. ControlHandle    CItem;                            /* Control handle */
  332.  
  333.     GetDItem(theDialog,ItemID,&DType,&DItem,&tempRect);/* Get the item handle and size */
  334.     CItem = (ControlHandle)DItem;                /* Change to control handle */
  335.     if (SizeIt)                                    /* Have to resize all CDEF connected controls */
  336.         SizeControl(CItem, tempRect.right-tempRect.left, tempRect.bottom-tempRect.top);/* Size it */
  337.     *thePosition = tempRect;                    /* Pass back the zone location and size */
  338.     if (ExtraData != NIL)                        /* See if extra data for a CDEF */
  339.         (*CItem)->contrlData = (Handle)ExtraData;    /* Send it */
  340.     if (StringID != 0) {                        /* See if a CDEF and needs the title set again*/
  341.         GetIndString(sTemp,StringID,1);            /* Get the string */
  342.         SetCTitle(CItem,sTemp);                    /* Set the string */
  343.     }
  344.     if (EnableIt)                                /* See if enable or disable the zone */
  345.         HiliteControl (CItem,0);                /* Enable the zone */
  346.     else
  347.         HiliteControl (CItem,255);                /* Dim the zone */
  348.         if (SetTheMax)
  349.     SetCtlMax(CItem,12345);                        /* Set the flag to the CDEF */
  350.     if (ShowIt)
  351.         ShowControl(CItem);                        /* Show it to activate it */
  352. }
  353.  
  354. /* ======================================================= */
  355.  
  356. /* Routine: CenterOnColorScreen */
  357. /* Purpose: Center a window onto the deepest color screen */
  358.  
  359. void CenterOnColorScreen (WindowPtr theWindow )
  360. {
  361. Rect        tempRect,gdRect,temp2Rect;                /* Temporary rectangle */
  362. GDHandle    theDevice;                                /* Graphic device */
  363.  
  364. if (theWindow != NIL) {    /* Make sure it is safe to go ahead */
  365.     SetRect(&tempRect,-32000,-32000,32000,32000);    /* Look in all directions */
  366.     theDevice = GetMaxDevice(&tempRect);            /* Get the deepest device */
  367.  
  368.     if (theDevice != NIL)                            /* Make sure we got a device */
  369.         {
  370.         gdRect = (*theDevice)->gdRect;                /* Get the device rect */
  371.         temp2Rect = theWindow->portRect;            /* Get the window rect */
  372.  
  373.         tempRect.top = gdRect.top + ((gdRect.bottom-gdRect.top)/2) - ((temp2Rect.bottom-temp2Rect.top)/2);
  374.         tempRect.left = gdRect.left + ((gdRect.right-gdRect.left)/2) - ((temp2Rect.right-temp2Rect.left)/2);
  375.         MoveWindow(theWindow, tempRect.left, tempRect.top, TRUE);/* Move the window*/
  376.         SetPort(theWindow);                         /* Prepare to write into our window */
  377.         }                                        /* End of IF */
  378.     }                                            /* End of IF */
  379. }
  380.  
  381. /* ======================================================= */
  382.  
  383. /* Routine: GetDeepestColorScreenRect */
  384. /* Purpose: Get the deepest color screen rect */
  385.  
  386. void GetDeepestColorScreenRect(Rect *DeepRect )
  387. {
  388. Rect        tempRect;                        /* Temporary rectangle */
  389. GDHandle    theDevice;                        /* Graphic device */
  390.  
  391.     *DeepRect = qd.screenBits.bounds;        /* Default to the main screen */
  392.     theDevice = NIL;                        /* Default to no device */
  393.     SetRect(&tempRect,-32000,-32000,32000,32000);    /* Look in all directions */
  394.     theDevice = GetMaxDevice(&tempRect);    /* Get the deepest device */
  395.  
  396.     if (theDevice != NIL) {                    /* Make sure we got a device */
  397.         *DeepRect = (*theDevice)->gdRect;    /* Get the device rect */
  398.     }
  399. }
  400. /* ======================================================= */
  401.